package com.anjho.bean;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

import javax.faces.application.FacesMessage;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.RequestScoped;
import javax.faces.context.FacesContext;
import javax.faces.event.ActionEvent;

import com.anjho.dao.DAOGenre;
import com.anjho.exceptions.InvalidGenreException;
import com.anjho.pojo.Genre;
import org.eclipse.persistence.exceptions.DatabaseException;

@ManagedBean(name = "genreBean")
@RequestScoped
public class GenreBean extends GenericBean implements Serializable {

	private static final long serialVersionUID = -1219853811120929598L;
	private List<Genre> genreList;
	private Genre genre;
	private String description;
	private DAOGenre daoGenre;

	public GenreBean() {
		super();
		this.genre = new Genre();
		this.genreList = new ArrayList<Genre>();
		daoGenre = new DAOGenre();
	}

	public void setGenreList(List<Genre> genreList) {
		this.genreList = genreList;
	}

	public List<Genre> getGenreList() throws Exception {
		synchronized (this) {
			genreList = daoGenre.getGenreList();
			return genreList;
		}
	}

	public Genre getGenre() {
		return genre;
	}

	public void setGenre(Genre genre) {
		this.genre = genre;
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		this.description = description;
	}

	public void onDeleteClick(ActionEvent actionEvent) {
		try {
			if ((this.genre == null) || (this.genre.getDescription().equals(""))){
				throw new InvalidGenreException("É necessário selecionar um gênero antes de executar esta operação!");
			}
			daoGenre.delete(this.genre);
			this.genre = new Genre();
		} catch (DatabaseException ex){
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_INFO, "Erro ao excluir", "Verifique se o gênero selecionado não possui produtos vinculados!");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (SQLException ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro na base de dados", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (InvalidGenreException ex){
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Gênero inválido", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, "Erro desconhecido", ex.getMessage());
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onSaveClick() {
		try {
			daoGenre.update(this.genre);
		} catch (Exception e) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, null, "Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

	public void onRegisterClick() {
		try {
			Genre aux = new Genre();
			aux.setDescription(description);
			daoGenre.insert(aux);
			this.description = "";
		} catch (Exception ex) {
			FacesMessage facesMsg = new FacesMessage(FacesMessage.SEVERITY_FATAL, null,	"Houve um erro não identificado.");
			FacesContext.getCurrentInstance().addMessage(null, facesMsg);
		}
	}

}
